package 个人密码库.view;


import 个人密码库.model.Password;
import 个人密码库.service.PasswordService;
import 个人密码库.view.inputview.ConsoleInput;

import java.security.GeneralSecurityException;
import java.sql.SQLException;
import java.util.Map;
import java.util.Scanner;

public class UserView {
    private final PasswordService passwordService;
    private final ConsoleInput consoleInput;

    public UserView(Scanner scanner, Map<String, String> config) {
        consoleInput = new ConsoleInput(scanner);
        passwordService = new PasswordService(config);
    }

    // 可优化：输入"help"可以重新显示菜单
    public void showMenu() {
        System.out.println("""
                [1] 查询所有密码
                [2] 查询某个密码
                [3] 存入一个新密码
                [4] 批量存入多个密码
                [5] 删除一个密码
                [6] 批量删除密码
                [7] 清空密码库
                [8] 修改某个密码
                [9] 退出
                """);
    }

    public void operate() throws SQLException, GeneralSecurityException {
        showMenu();
        while (true) {
            int operationNumber = consoleInput.getOperationNumber();
            switch (operationNumber) {
                case 1 -> {
                    // 此处可优化：可以先打印数据，再询问是否需要打印到表格
                    passwordService.setShouldExportToExcel(consoleInput.askIfNeedToOperate("是否需要打印到Excel表格（yes/no）？"));
                    passwordService.getAllPasswords();
                }

                case 2 -> {
                    passwordService.setShouldExportToExcel(consoleInput.askIfNeedToOperate("是否需要打印到Excel表格（yes/no）？"));
                    passwordService.getSomePasswords(consoleInput.getKeyWord());
                }
                case 3 -> {
                    Password password = consoleInput.getPassword();
                    passwordService.storeOnePassword(password);
                }
                case 4 -> passwordService.insertAllPasswords();
                case 5 -> passwordService.deleteOnePassword(consoleInput.getId());
                case 6 -> passwordService.deleteSomePasswords(consoleInput.getSomeIds());
                case 7 -> passwordService.clear();
                case 8 -> {
                    if (consoleInput.askIfNeedToOperate("是否需要修改密码（yes/no）？")) {
                        passwordService.updatePassword(consoleInput.getId(), consoleInput.inputAPassword());
                    }
                    if (consoleInput.askIfNeedToOperate("是否需要修改描述信息（yes/no）？")) {
                        passwordService.updateDescription(consoleInput.getId(), consoleInput.getDescription());
                    }
                }
                case 9 -> {
                    if (consoleInput.askIfNeedToOperate("您确定要退出吗？（yes/no）")){
                        System.out.println("！！！请保管好已生成的密码表！！！");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {}
                        System.out.println("退出！");
                        return;
                    }
                }
            }
        }
    }
}
